#include "logindialog.h"
#include "ui_logindialog.h"
#include <QMessageBox>


//郝&戴代码


LoginDialog::LoginDialog(UserType loginType, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog),
    loginType(loginType)
{
    ui->setupUi(this);
    // 设置默认账号
    ui->usernameEdit->setText("100");
    ui->passwordEdit->setText("100");
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

QVector<PriceQuantity> LoginDialog::getBidData() const
{
    return bidData;
}

void LoginDialog::on_loginButton_clicked()
{
    QString username = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();
    
    // 检查是否为空
    if (username.isEmpty() || password.isEmpty()) {
        ui->messageLabel->setText("用户名和密码不能为空");
        return;
    }
    
    // 检查默认账号或已注册账号
    if ((username == "100" && password == "100") || 
        (username == registeredUser && password == registeredPass)) {
        ui->messageLabel->setText("");
        QMessageBox::information(this, "登录成功", "欢迎使用电力交易系统！");
        
        // 登录成功后显示竞价提交界面
        BidWindow bidWindow(loginType, this);
        if (bidWindow.exec() == QDialog::Accepted) {
            bidData = bidWindow.getSortedData();
            accept();
        }
    } else {
        ui->messageLabel->setText("用户名或密码错误");
    }
}

void LoginDialog::on_registerButton_clicked()
{
    QString username = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();
    
    if (username.isEmpty() || password.isEmpty()) {
        ui->messageLabel->setText("用户名和密码不能为空");
        return;
    }
    
    // 保存注册信息
    registeredUser = username;
    registeredPass = password;
    
    QMessageBox::information(this, "注册成功", "账号注册成功，请登录！");
}
